import {
	createMarketInfo,
	deleteMarketInfoById,
	getMarketInfoById,
	queryMarketListByCId,
	updateMarketInfo
} from '../../services/market/market';
import {message} from "antd";
import Constants from '../../utils/Constants';

const R = require('ramda');

export default {
	namespace: 'market',
	state: {
		page: {},
		dataSource: {},
		marketInfosList: []
	},
	reducers: {

        /**
		 * 设置营销线索数据列表
         */
		setMarketInfosList(state, { payload }) {
			return { ...state, marketInfosList: payload.list, page: payload.page }
		},

		/**
		 * 设置当前查询的营销线索
		 */
		setMarket(state, { payload }) {
			return { ...state, dataSource: payload.dataSource }
		},

		/**
		 * 新增营销线索前把当前state的营销线索清除
		 */
		cleanDataSource(state) {
			return { ...state, dataSource: {} }
		},
	},
	effects: {
        /**
         * 分页查询客户营销线索列表
         */
		*queryMarketListByCId({ payload }, { call, put, select }) {
			let page = yield select(state => state.market.page);
			page = payload.page ? payload.page : page; //首次查询可以用默认的分页信息
			const res = yield call(queryMarketListByCId, {
				cId: payload.cId,
				page: page
			});

			if (res && res.data.resultCode === "ok") {
				let content = res.data.content;
				let list = R.isNil(content.list) ? [] : content.list;
				yield put({
					type: 'setMarketInfosList',
					payload: {
						list: list,
						page: R.isNil(content.page) ? Constants.DEFAULT_PAGE : content.page //列表为空的时候重置state的分页信息
					}
				});
				if (payload.onSuccess) payload.onSuccess(list); //查询成功的回调函数
			} else {
				if (payload.onFail) payload.onFail(); //查询失败的回调函数
			}
		},

        /**
         * 根据id获取营销线索
         */
		*getMarketInfoById({ payload }, { call, put, select }) {
			if (payload.id) {
				const res = yield call(getMarketInfoById, { id: payload.id });

				if (res && res.data.resultCode === "ok") {
					let dataSource = R.isNil(res.data.content) ? {} : res.data.content;
					yield put({
						type: "setMarket",
						payload: {
							dataSource: dataSource
						}
					});
					if (payload.onSuccess) payload.onSuccess(dataSource);
				} else {
					if (payload.onFail) payload.onFail();
				}
			} else {
				yield put({
					type: "cleanDataSource"
				});
				if (payload.onSuccess) payload.onSuccess();
			}

		},


        /**
         * 新增工作追踪
         */
		*createMarketInfo({ payload }, { call, select, put }) {
			const res = yield call(createMarketInfo, { dataSource: payload.dataSource });

			if (res && res.data.resultCode === "ok") {
				message.info("营销线索新增成功！");
				if (payload.onSuccess()) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();

			}
		},


        /**
         * 删除营销线索
         */
		*deleteMarketInfoById({ payload }, { call, put, select }) {
			const res = yield call(deleteMarketInfoById, { id: payload.id });

			if (res && res.data.resultCode === "ok") {
				if (payload.onSuccess()) payload.onSuccess();
				message.info("营销线索删除成功！");
			} else {
				if (payload.onFail) payload.onFail();
			}
		},


        /**
         * 修改营销线索
         */
		*updateMarketInfo({ payload }, { call, select, put }) {
			const res = yield call(updateMarketInfo, { dataSource: payload.dataSource });
			if (res && res.data.resultCode === "ok") {
				message.info("营销线索更新成功");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},
	},
	subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};